﻿using System;
using System.Linq;
using System.Collections.Generic;
using MobilePal.Log;
using MobilePal.ProcessorAgents;
using MobilePal.State;

namespace MobilePal
{
    public class Processor
    {
        private ApplicationState _applicationState;

        public List<ILogger> Loggers { get; set; }

        private readonly List<IProcessorAgent> _processorAgents = new List<IProcessorAgent>();               

        public Processor(List<ILogger> loggers, ApplicationState applicationState)
        {
            _applicationState = applicationState;
            Loggers = loggers;
        }

        public void AttachAgent(IProcessorAgent agent)
        {
            if (_processorAgents.Contains(agent))
                throw new ArgumentException("Agent already attached");

            _processorAgents.Add(agent);

            agent.AttachToState(Loggers, _applicationState);
        }

        public void DetachAgent(IProcessorAgent agent)
        {
            if (!_processorAgents.Contains(agent))
                throw new ArgumentException("No agent attached");

            _processorAgents.Remove(agent);

            agent.DetachFromState(_applicationState);
        }

        public void DetachAllAgents()
        {
            foreach (var processorAgent in _processorAgents)
            {
                processorAgent.DetachFromState(_applicationState);
            }
            _processorAgents.Clear();
        }

    }
}
